<template>
	<view>
		<u-subsection :list="navList" :current="currentNav" @change="changeContentType"></u-subsection>
		<view v-if="dataList.length <= 0">
			<u-empty mode="message"></u-empty>
		</view>
		<view v-else>
			<uni-list :border="true">
				<!-- 头像显示圆点 -->
				<uni-list-chat :title="item.noticeTitle" v-for="(item,index) in dataList" :key="index" clickable
					:avatar="baseUrl+item.sysUser.avatar" note="快来点进来看看叭" @click="toPage(item)"
					:time="formatTime(item.createTime,'{y}-{m}-{d}')" badge-positon="left"
					:badge-text="item.read"></uni-list-chat>
				<u-loadmore status="nomore" />
			</uni-list>
		</view>
		<u-toast ref="uToast" />
	</view>
</template>
<script>
	import {
		getNoticeList,
		removeNotice,
		updateNotice
	} from '@/api/bot/notice'
	import {
		getDailyList
	} from '@/api/bot/daily'
	import {
		getEatingList
	} from '@/api/bot/eating'
	import {
		getMoneyList
	} from '@/api/bot/money'
	import {
		getBillCellById,
		getBillSimpleById
	} from '@/api/bot/bill'
	import config from '@/config'
	export default {
		data() {
			return {
				baseUrl: config.baseUrl,
				navList: [{
						name: '系统通知'
					},
					{
						name: '个人通知'
					}
				],
				currentNav: 0,
				query: {
					pageNum: 1,
					pageSize: 12,
					systemNotice: 0,
					groupId: 0
				},
				dataList: [],
				total: 0
			}
		},
		onShow() {
			uni.getStorage({
				key: 'groupIdByIndex',
				success: (res) => {
					let data = JSON.parse(res.data);
					this.query.groupId = data.id
					this.reset();
				}
			})
		},
		onReachBottom() {
			if (this.total < 12) {
				return
			} else {
				this.loadNextPageData();
			}
		},
		onPullDownRefresh() {
			this.reset()
			setTimeout(() => {
				uni.stopPullDownRefresh()
			}, 1500)
		},
		methods: {
			reset() {
				this.query.pageNum = 1
				this.dataList = []
				this.getNoticeList()
			},
			loadNextPageData() {
				this.query.pageNum++;
				this.getNoticeList()
			},
			getNoticeList() {
				uni.showLoading({
					title: "加载中"
				})
				getNoticeList(this.query).then(res => {
					res.rows.forEach(element => {
						element.createTime = new Date(element.createTime).getTime()
						if (element.isRead === 0) {
							element.read = 'dot'
						} else {
							element.read = ''
						}
					})
					this.dataList = this.dataList.concat(res.rows);
					this.total = res.total
					uni.hideLoading()
				})
			},
			changeContentType(index) {
				this.currentNav = index; // 更新 currentNav 的值
				this.query.systemNotice = index;
				this.reset()
			},
			toPage(obj) {
				if (obj.isRead !== 1) {
					obj.isRead = 1;
					updateNotice(obj).then(res => {
						console.log(res.code == 200 ? '已读' : '未读');
					})
				}
				const noticeType = obj.noticeType
				// 日常发布
				if (noticeType == 1 || noticeType == 5) {
					let query = {
						pageNum: 1,
						pageSize: 10,
						id: obj.refId
					}
					getDailyList(query).then(res => {
						if (res.total <= 0) {
							this.remove(obj.id)
							return;
						}
						const processedList = res.rows.map(element => {
							element.avatar = this.baseUrl + element.avatar;
							element.likeList = element.botDailyLikes.map(item => item.nickName).join(',')
							element.isLike = element.botDailyLikes.length > 0 ? element
								.botDailyLikes.some(
									item => item.userId == this.$store.getters.userId) : false;
							return element;
						});
						let data = processedList[0]
						data.path = 'index'
						const formDataStr = JSON.stringify(data);
						this.$tab.navigateTo('/pages/detail/dailyDetail?data=' + encodeURIComponent(formDataStr))
					})
					// 干饭发布	
				} else if (noticeType == 2) {
					let query = {
						pageNum: 1,
						pageSize: 10,
						id: obj.refId
					}
					getEatingList(query).then(res => {
						if (res.total <= 0) {
							this.remove(obj.id)
							return;
						}
						this.openExtend(res.rows[0])
					})
					// 钱袋子发布
				} else if (noticeType == 3) {
					let query = {
						pageNum: 1,
						pageSize: 10,
						id: obj.refId
					}
					getMoneyList(query).then(res => {
						if (res.total <= 0) {
							this.remove(obj.id)
							return;
						}
						let data = res.rows[0]
						data.path = 'index';
						const formDataStr = JSON.stringify(data);
						this.$tab.navigateTo('/pages/detail/moneyDetail?formData=' + encodeURIComponent(
							formDataStr))
					});
					// 清单发布
				} else if (noticeType == 4) {
					let query = {
						id: obj.refId
					}
					getBillSimpleById(query).then(res => {
						if (!res.data) {
							this.remove(obj.id)
							return;
						}
						this.$store.commit('SET_PUBLISHCONTENTTYPE', 3)
						this.$store.commit('IS_REFRESH', true)
						this.$tab.switchTab('/pages/index')
					})
					// 待办处理
				} else if (noticeType == 6) {
					let query = {
						id: obj.refId
					}
					getBillCellById(query).then(res => {
						if (!res.data) {
							this.remove(obj.id)
							return;
						}
						this.$tab.navigateTo(
							`/pages/detail/billDetail?billId=${res.data.botBillId}&cellId=${obj.refId}`
						)
					})
					// 审核处理
				} else if (noticeType == 7) {
					this.$tab.navigateTo(
						`/pages/mine/bot/ownGroup/ownGroup`
					)
				}

			},
			// 干饭功能：预览
			openExtend(obj) {
				let content = obj.sysFileInfos.map(item => item.fileUrl)
				uni.previewImage({
					urls: content,
					current: 0
				});
			},
			remove(id) {
				let query = {
					id: id
				}
				this.$modal.confirm('消息已过期，是否删除？').then(() => {
					removeNotice(query).then(res => {
						if (res.code === 200) {
							this.reset()
							this.$modal.msgSuccess("删除成功")
						} else {
							this.$modal.msgError("删除失败")
						}
					})
				});
			}
		},
	}
</script>

<style>
</style>